package zuoye.xiti;
public class BreakfastThread implements Runnable {
    static Thread MrZhang, MrLi, MrWang;
    int sumFive = 3, sumTwenty = 0, sumTen = 0;
    public synchronized void sellBreakfast(int money) {
        if (money == 5) {
            sumFive = sumFive + 1;
            System.out.println("李先生，您给的钱数正好。");
        } else if (money == 10) {
            while (sumFive < 1) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
            sumFive = sumFive - 1;
            sumTen = sumTen + 1;
            System.out.println("王先生，您给我10元，找您5元。");
        } else if (money == 20) {
            while (sumFive < 3) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
            sumFive = sumFive - 3;
            sumTwenty = sumTwenty + 1;
            System.out.println("张先生，您给我20元，找您15元。");
        }
        notifyAll();
    }
    public void run() {
        if (Thread.currentThread() == MrZhang) {
            sellBreakfast(20);
        } else if (Thread.currentThread() == MrLi) {
            sellBreakfast(5);
        } else if (Thread.currentThread() == MrWang) {
            sellBreakfast(10);
        }
    }
    public static void main(String[] args) {
        BreakfastThread t = new BreakfastThread();
        MrZhang = new Thread(t);
        MrWang = new Thread(t);
        MrLi = new Thread(t);
        MrZhang.start();
        MrWang.start();
        MrLi.start();
    }
}
